package com.yxuan.springboot.service.impl;

import com.yxuan.springboot.mapper.*;
import com.yxuan.springboot.model.*;
import com.yxuan.springboot.service.TestTypeDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

/**
 * 
 * @author zchao
 *    TestTypeDetails业务层实现类
 * @date 2023-05-16 14:22:56
 */
@Service
@Transactional
public class TestTypeDetailsServiceImpl extends BaseServiceImpl<TestTypeDetails> implements TestTypeDetailsService {
	@Autowired
    private TestTypeDetailsMapper testTypeDetailsMapper;

    @Autowired
    private TestBlockMapper testBlockMapper;

    @Autowired
    private TestVersionTypeMapper testVersionTypeMapper;

    @Autowired
    private TestReleaseMapper testReleaseMapper;

    @Autowired
    private TestVersionTopicMapper testVersionTopicMapper;

    @Autowired
    private TestQuestionsMapper testQuestionsMapper;

    @Autowired
    private TestOptionsMapper testOptionsMapper;

//    测试首页数据
    @Override
    public HashMap<String, Object> testHome() throws Exception {
        HashMap<String, Object> result1 = new HashMap<>();
        HashMap<String, Object> result2 = new HashMap<>();
        TestTypeDetails testTypeDetail = new TestTypeDetails();
        testTypeDetail.setHot("1");
//         获取头部的热点测试
        TestTypeDetails testTypeDetail2 = testTypeDetailsMapper.queryObjectByPramas(testTypeDetail);
        result1.put("id",testTypeDetail2.getId());
        result1.put("testname",testTypeDetail2.getTypeName());
        result1.put("pic",testTypeDetail2.getPic());
        result1.put("slogan",testTypeDetail2.getSlogan());
        result1.put("join_num",testTypeDetail2.getJoinNum());
        result2.put("head",result1);
//         获取区块以及区块包含的类型
        List list1 = new ArrayList();
        List<TestBlock> testBlocks = testBlockMapper.queryAll();
        for (TestBlock testBlock : testBlocks) {
            HashMap<String, Object> result3 = new HashMap<>();
            result3.put("block_id", testBlock.getId());
            result3.put("block_name", testBlock.getBlockName());
            TestTypeDetails testTypeDetail1 = new TestTypeDetails();
            testTypeDetail1.setTypeBlockid(testBlock.getId());
            List<TestTypeDetails> testTypeDetails = testTypeDetailsMapper.queryListByPramas(testTypeDetail1);
            List list2 = new ArrayList();
            for (TestTypeDetails typeDetail : testTypeDetails) {
                HashMap<String, Object> result4 = new HashMap<>();
                result4.put("id", typeDetail.getId());
                result4.put("testname", typeDetail.getTypeName());
                result4.put("pic", typeDetail.getPic());
                result4.put("slogan", typeDetail.getSlogan());
                result4.put("join_num", typeDetail.getJoinNum());
                list2.add(result4);
            }
            result3.put("test_list", list2);
            list1.add(result3);
        }
        result2.put("block",list1);

        return result2;
    }

//    准备测试界面数据
//     点击测试详情，进入准备测试界面：test_type_detils——id、type_name、introduce、pic
//          test_version_type:根据typeid查找version，where using=1
//          test_release:根据 version 和 type_detils_id 查找对应的 topic_num、test_time、illustrate
    @Override
    public HashMap<String, Object> prepareTest(Integer id) throws Exception {
        HashMap<String,Object> result = new HashMap<>();
        TestTypeDetails testTypeDetails = new TestTypeDetails();
        testTypeDetails.setId(id);
        System.out.println(testTypeDetails);
        TestTypeDetails testTypeDetail = testTypeDetailsMapper.queryObjectByPramas(testTypeDetails);
        result.put("id",id);
        result.put("testname",testTypeDetail.getTypeName());
        result.put("introduce", testTypeDetail.getIntroduce());
        result.put("pic",testTypeDetail.getPic());
//        test_version_type:根据typeid查找version，where using=1
        TestVersionType testVersionType = new TestVersionType();
        testVersionType.setTypeId(id);
        testVersionType.setTestUsing("1");
        TestVersionType testVersionType1 = testVersionTypeMapper.queryObjectByPramas(testVersionType);
        TestRelease testRelease = new TestRelease();
        testRelease.setVersion(testVersionType1.getVersion());
        TestRelease testRelease1 = testReleaseMapper.queryObjectByPramas(testRelease);
        result.put("topic_number",testRelease1.getTopicNum());
        result.put("test_time",testRelease1.getTestTime());
        result.put("illustrate",testRelease1.getIllustrate());
        result.put("version",testRelease1.getVersion());
//        TODO 使用treemap有序存储题目以及答案
//        通过 version 和 typeid 找到topic_id
        TestVersionTopic testVersionTopic = new TestVersionTopic();
        testVersionTopic.setVersion(testRelease1.getVersion());
        testVersionTopic.setTypeId(id);
        List<TestVersionTopic> testVersionTopicslist = testVersionTopicMapper.queryListByPramas(testVersionTopic);
//        遍历列表查找题目,
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < testVersionTopicslist.size();i++){
            HashMap<Object, Object> result1 = new HashMap<>();
            TestVersionTopic testVersionTopic1 = testVersionTopicslist.get(i);

            TestQuestions testQuestion = new TestQuestions();
            testQuestion.setId(testVersionTopic1.getTopicId());
            TestQuestions testQuestion1 = testQuestionsMapper.queryObjectByPramas(testQuestion);
            result1.put("id",testQuestion1.getId());
            result1.put("content",testQuestion1.getContent());
            result1.put("question_pic",testQuestion1.getQuestionPic());
//            通过题目id来查找答案
            TestOptions option = new TestOptions();
            option.setQuestionId(testQuestion1.getId());
            List<TestOptions> testOptionsList = testOptionsMapper.queryListByPramas(option);
//            排序
            for(int m = 0;m < testOptionsList.size()-1;m++){
                for (int n = m+1;n < testOptionsList.size();n++){
                    int i1 = (testOptionsList.get(m).getOptionName().charAt(0) - '0');
                    int i2 = (testOptionsList.get(n).getOptionName().charAt(0) - '0');
                    if (i1 > i2){
                        TestOptions options = testOptionsList.get(m);
                        testOptionsList.set(m,testOptionsList.get(n));
                        testOptionsList.set(n,options);
                    }
                }
            }


            List<Object> list1 = new ArrayList<>();
            for(TestOptions j : testOptionsList){
                TreeMap<String, Object> result2 = new TreeMap<>();
                result2.put("id",j.getId());
                result2.put("optionName",j.getOptionName());
                result2.put("optionScore",j.getOptionScore());
                result2.put("optionContent",j.getOptionContent());
                list1.add(result2);
            }
            result1.put("options",list1);
            list.add(result1);
        }
        result.put("title",list);
        return result;
    }
}
